<template>
  <div>
    <el-dialog :title="showTitle" :visible="isShowDialog" :destroy-on-close="true" @close="btnCancel">
      <!-- 表单组件  el-form   label-width设置label的宽度   -->
      <!-- 匿名插槽 -->
      <el-form ref="addForm" label-width="120px" :model="formData" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @blur="changeManager">
            <el-option
              v-for="user in employeeList"
              :key="user.id"
              :value="user.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <!-- 列被分为24 -->
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
          <el-button size="small" @click="btnCancel">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog></div>
</template>

<script>
import { getDepartment, addDepartment, getDepartmentDetail, editDepartment } from '@/api/department'
import { getEmployeeSimble } from '@/api/employee'
export default {
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkRepeatName = async(rule, val, callback) => {
      // 校验是否重复
      // 获取全部部门列表
      const { depts } = await getDepartment()
      let isRepeat
      if (this.formData.id) {
        // 编辑
        isRepeat = depts
        // 所有部门列表中,筛选出当前点击的子部门
        // 当前编辑的部门需要以父部门id(pid)作为同级判断条件
          .filter(item => item.pid === this.treeNode.pid)
          // 查找是否有任何一个名字与当前输入值一样
          .some(item => item.name === val && item.id !== this.treeNode.id)
      } else {
        isRepeat = depts
        // 所有部门列表中, 筛选出当前点击的子部门
          .filter(item => item.pid === this.treeNode.id)
        // 查找是否有任何一个名字跟当前输入值一样
          .some(item => item.name === val)
      }

      // 根据结果处理
      if (isRepeat) {
        callback(new Error('同一父部门下名字必须唯一'))
      } else {
        callback()
      }
    }
    const checkRepeatCode = async(rule, val, callback) => {
      const { depts } = await getDepartment()
      let isRepeat
      if (this.formData.id) {
        // 编辑
        isRepeat = depts.some(item => item.code === val && item.id !== this.treeNode.id)
      } else {
        // 新增
        isRepeat = depts.some(item => item.code === val)
      }
      isRepeat ? callback(new Error('部门编码必须唯一')) : callback()
    }
    return {
      formData: {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      },
      employeeList: [],
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: checkRepeatName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: checkRepeatCode, trigger: 'blur' }
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增子部门'
    }
  },
  async created() {
    this.employeeList = await getEmployeeSimble()
    // console.log(96, this.employeeList)
  },
  methods: {
    changeManager() {
      setTimeout(() => {
        this.$refs.addForm.validateField('manager')
      }, 100)
    },
    async btnOk() {
      // 校验表单
      await this.$refs.addForm.validate()
      // 发送请求
      if (this.formData.id) {
        // 编辑
        await editDepartment(this.formData)
      } else {
        // 新增
        await addDepartment({
          ...this.formData,
          pid: this.treeNode.id
        })
      }
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      // 通知父组件修改isShowDialog的值
      this.$emit('update:isShowDialog', false)
      // 更新页面
      this.$emit('addDepartment')
    },
    btnCancel() {
      // 清空表单数据
      this.formData = {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      }
      // 清空表单校验
      this.$refs.addForm.resetFields()
      // 确保弹窗关闭
      this.$emit('update:isShowDialog', false)
    },
    async getDepart(id) {
      this.formData = await getDepartmentDetail(id)
    }
  }
}
</script>

<style>

</style>
